package com.zretc.controller;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zretc.entity.Collect;
import com.zretc.entity.PlayList;
import com.zretc.entity.Song;
import com.zretc.service.PlayListService;
import com.zretc.service.SongService;
import com.zretc.service.impl.PlayListServiceImpl;
import com.zretc.service.impl.SongServiceImpl;

/**
 * Servlet implementation class PlayListServlet
 */
@WebServlet("/PlayListServlet")
@MultipartConfig
public class PlayListServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private PlayListService playListService = new PlayListServiceImpl();
    Gson gson = new Gson();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PlayListServlet() {
        super();
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String op = request.getParameter("op");
		if ("deleteMulitPlaySong".equals(op)) {
			doDeleteMulitPlaySong(request,response);
		}else if ("getSongComment".equals(op)) {
			doGetSongComment(request,response);
		}else if ("existPlayList".equals(op)) {
			doExistPlayList(request,response);
		}else if ("addPlayList".equals(op)) {
			doAddPlayList(request,response);
		}
	}
	
	private void doAddPlayList(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String accountId = request.getParameter("accountId");
		String songId = request.getParameter("songId");
		int addPlayBySongIdAndAccountId = playListService.addPlayBySongIdAndAccountId(Integer.parseInt(songId), Integer.parseInt(accountId));
		if (addPlayBySongIdAndAccountId>0) {
			response.getWriter().write("addyes");
		}else{
			response.getWriter().write("addno");
		}
	}

	private void doExistPlayList(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String accountId = request.getParameter("accountId");
		String songId = request.getParameter("songId");
		PlayList playBySongIdAndAccountId = playListService.getPlayBySongIdAndAccountId(Integer.parseInt(songId), Integer.parseInt(accountId));
		if (playBySongIdAndAccountId==null) {
			response.getWriter().write(songId);
		}else if (playBySongIdAndAccountId!=null) {
			response.getWriter().write("yesPlay");
		}
	
	}

	//点击评论按钮，获取当前歌曲信息
	private void doGetSongComment(HttpServletRequest request, HttpServletResponse response) throws IOException {
	/*	String songName = request.getParameter("songName");
		List<Song> songBySongName = songService.getSongBySongName(songName);
		Integer songId = songBySongName.get(0).getSongId();*/
		Integer songId = Integer.parseInt(request.getParameter("songId"));
		response.sendRedirect("front/comment.jsp?songId="+songId);
	}
	
	//删除播放列表歌曲
	private void doDeleteMulitPlaySong(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Map<String, Object> map = new HashMap<String, Object>();
		String accountId = request.getParameter("accountId");
		map.put("accountId", accountId);
		String song = request.getParameter("song");
		Type type = new TypeToken<ArrayList<Song>>(){}.getType();
		ArrayList<Song> songList = gson.fromJson(song, type);
		int n = 0;
		for (int i = 0; i < songList.size(); i++) {
			Integer songId = songList.get(i).getSongId();
			int deletePlaySong = playListService.deletePlaySong(songId, Integer.parseInt(accountId));
			if (deletePlaySong>0) {
				n++;
			}
		}
		if (n>0) {
			response.getWriter().write("yes");
		}else {
			response.getWriter().write("no");
		}
		
	}

}
